home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / System Extensions / Macintosh Drag and Drop 1.1.1 / Demo Applications / DragText Sources / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-15  |  4.3 KB  |  232 lines  |  [TEXT/KAHL]

  1. /*
  2.  *
  3.  *        file.c
  4.  *
  5.  *        File Input/Output routines.
  6.  *        
  7.  *
  8.  *        Author:        Rob Johnston
  9.  *        Date:        Thursday, January 26, 1992
  10.  *
  11.  *        Copyright © 1992 Apple Computer, Inc.
  12.  *
  13.  */
  14.  
  15. #include "globals.h"
  16. #include "prototypes.h"
  17. #include "resources.h"
  18.  
  19.  
  20. short ReadDocumentFile(Document *theDocument)
  21.  
  22. {    long        count;
  23.     short        theResult;
  24.     char        buffer[256];
  25.     TextStyle    theStyle;
  26.  
  27.     SetCursor(*GetCursor(watchCursor));
  28.  
  29.     TESetSelect(0, (**(theDocument->theTE)).teLength, theDocument->theTE);
  30.     TEDelete(theDocument->theTE);
  31.  
  32.     if (theResult = SetFPos(theDocument->fRefNum, fsFromStart, 0))
  33.         return(theResult);
  34.  
  35.     do {
  36.         count = 256;
  37.         theResult = FSRead(theDocument->fRefNum, &count, &buffer);
  38.         TEInsert(&buffer, count, theDocument->theTE);
  39.     } while (theResult == noErr);
  40.  
  41.     TESetSelect(0, 32767, theDocument->theTE);
  42.     theStyle.tsFont = 21;
  43.     theStyle.tsSize = 12;
  44.     TESetStyle(doFont + doSize, &theStyle, true, theDocument->theTE);
  45.     TESetSelect(0, 0, theDocument->theTE);
  46.     theDocument->dirty = false;
  47.  
  48.     return(noErr);
  49. }
  50.  
  51.  
  52. short WriteDocumentFile(Document *theDocument)
  53.  
  54. {    short        theResult;
  55.     long        length;
  56.     char        *bufPtr;
  57.  
  58.     SetCursor(*GetCursor(watchCursor));
  59.  
  60.     if (! theDocument->fRefNum)
  61.         return(fnOpnErr);
  62.  
  63.     if (theResult = SetFPos(theDocument->fRefNum, fsFromStart, 0))
  64.         return(theResult);
  65.  
  66.     length = (**(theDocument->theTE)).teLength;
  67.     bufPtr = *((**(theDocument->theTE)).hText);
  68.  
  69.     if (theResult = FSWrite(theDocument->fRefNum, &length, bufPtr))
  70.         return(theResult);
  71.  
  72.     if (theResult = SetEOF(theDocument->fRefNum, length))
  73.         return(theResult);
  74. }
  75.  
  76.  
  77. void DoNewDocument()
  78.  
  79. {    Document        *theDocument;
  80.  
  81.     if (theDocument = NewDocument()) {
  82.         ShowWindow(theDocument->theWindow);
  83.     }
  84. }
  85.  
  86.  
  87. OSErr DoOpenFile(FSSpec *theFile)
  88.  
  89. {    OSErr            result;
  90.     short            refNum;
  91.     Document        *theDocument;
  92.  
  93.     if (result = FSpOpenDF(theFile, fsRdWrPerm, &refNum))
  94.         return(result);
  95.  
  96.     if (theDocument = NewDocument()) {
  97.         theDocument->fRefNum = refNum;
  98.         if (ReadDocumentFile(theDocument)) {
  99.             SysBeep(1);
  100.         }
  101.         SetWTitle(theDocument->theWindow, theFile->name);
  102.         AdjustScrollBar(theDocument);
  103.         ShowWindow(theDocument->theWindow);
  104.     } else {
  105.         SysBeep(1);
  106.         FSClose(refNum);
  107.         return(memFullErr);
  108.     }
  109.     return(noErr);
  110. }
  111.  
  112.  
  113.  
  114. void DoOpenDocument()
  115.  
  116. {    short                refNum;
  117.     SFTypeList            theTypeList;
  118.     StandardFileReply    theReply;
  119.     Document            *theDocument;
  120.  
  121.     theTypeList[0] = 'TEXT';
  122.     StandardGetFile(0L, 1, theTypeList, &theReply);
  123.  
  124.     if (theReply.sfGood) {
  125.         DoOpenFile(&theReply.sfFile);
  126.  
  127. /*        if (FSpOpenDF(&theReply.sfFile, fsRdWrPerm, &refNum)) {
  128.             SysBeep(1);
  129.             return;
  130.         }
  131.         if (theDocument = NewDocument()) {
  132.             theDocument->fRefNum = refNum;
  133.             if (ReadDocumentFile(theDocument)) {
  134.                 SysBeep(1);
  135.             }
  136.             SetWTitle(theDocument->theWindow, theReply.sfFile.name);
  137.             AdjustScrollBar(theDocument);
  138.             ShowWindow(theDocument->theWindow);
  139.         } else {
  140.             SysBeep(1);
  141.             FSClose(refNum);
  142.         }
  143. */
  144.  
  145.     }
  146. }
  147.  
  148.  
  149. short DoSaveAsDocument(Document *theDocument)
  150.  
  151. {    short                theResult;
  152.     Str255                thePrompt, theName;
  153.     StandardFileReply    theReply;
  154.  
  155.     if (! theDocument)
  156.         return(false);
  157.  
  158.     GetIndString(&thePrompt, FileStringsID, slSavePromptIndex);
  159.     GetWTitle(theDocument->theWindow, &theName);
  160.     StandardPutFile(&thePrompt, &theName, &theReply);
  161.  
  162.     if (theReply.sfGood) {
  163.         if (!theReply.sfReplacing) {
  164.             theResult = FSpCreate(&theReply.sfFile, FileCreator, FileType, theReply.sfScript);
  165.             if (theResult) {
  166.                 SysBeep(1);
  167.                 return(false);
  168.             }
  169.         }
  170.         if (theDocument->fRefNum) {
  171.             theResult = FSClose(theDocument->fRefNum);
  172.         }
  173.         theResult = FSpOpenDF(&theReply.sfFile, fsRdWrPerm, &theDocument->fRefNum);
  174.         if (theResult) {
  175.             SysBeep(1);
  176.             return(false);
  177.         }
  178.  
  179.         if (theResult = WriteDocumentFile(theDocument)) {
  180.             SysBeep(1);
  181.             return(false);
  182.         }
  183.  
  184.         SetWTitle(theDocument->theWindow, theReply.sfFile.name);
  185.         theDocument->dirty = false;
  186.  
  187.     } else {
  188.         return(false);
  189.     }
  190.     return(true);
  191. }
  192.  
  193.  
  194. short DoSaveDocument(Document *theDocument)
  195.  
  196. {
  197.     if (! theDocument)
  198.         return(false);
  199.  
  200.     if (theDocument->fRefNum) {
  201.         if (WriteDocumentFile(theDocument)) {
  202.             SysBeep(1);
  203.             return(false);
  204.         } else {
  205.             theDocument->dirty = false;
  206.         }
  207.         return(true);
  208.     } else {
  209.         return(DoSaveAsDocument(theDocument));
  210.     }
  211. }
  212.  
  213.  
  214. void DoRevertDocument(Document *theDocument)
  215.  
  216. {    Str255        theName;
  217.  
  218.     if (! theDocument)
  219.         return;
  220.  
  221.     if (theDocument->fRefNum) {
  222.         GetWTitle(theDocument->theWindow, &theName);
  223.         ParamText(&theName, "", "", "");
  224.         if (Alert(idRevertALRT, 0L) == 1) {
  225.             if (ReadDocumentFile(theDocument))
  226.                 SysBeep(1);
  227.         }
  228.     }
  229. }
  230.  
  231.  
  232.